//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agreementData: [],
    // 申请成功后的
    operaterId: "",
    payMoney: "",
    orderNumber: "",
    payFlag: true,
    payType: "",
    // 
    uid: "",
    userType: "",
    // 
    opacity: "0.5",
    agreeAndReceipt: "0",
    isPartnerEnterIsDefaultPhone: "",
    partnerEnterDefaultPhone: "",
    operaterPhone: "",
    // 输入支付密码
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
    })
    this.GetMyPartnerInfo();
    this.GetUserInfoByIDs();

  },

  //显示支付密码输入层
  showInputLayer: function() {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function() {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: ''
    });

  },
  // 获取焦点
  getFocus: function() {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function(e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //输入密码 确定 
  surePasswords: function() {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=PayPartnerOrder&OrderNumber=" + orderNumber + "&Type=0"),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data));
              console.log(json);
              if (json.ResultFlag == 1) {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 
  operaterPhone: function(e) {
    this.setData({
      operaterPhone: e.detail.value,
    })
  },
  // 
  GetUserInfoByIDs: function() {
    var that = this;
    var uid = this.data.uid;
    var operaterPhone = this.data.operaterPhone;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserInfoByIDs&UID=" + uid),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        // console.log(json);
        if (json.ResultFlag == 1) {
          if (json.ResultData.IsPartnerEnterIsDefaultPhone == 1) {
            operaterPhone = json.ResultData.PartnerEnterDefaultPhone;
          } else {}
          that.setData({
            isPartnerEnterIsDefaultPhone: json.ResultData.IsPartnerEnterIsDefaultPhone,
            partnerEnterDefaultPhone: json.ResultData.PartnerEnterDefaultPhone,
            operaterPhone: operaterPhone,
          })
        }
      }
    })

  },
  // 
  GetMyPartnerInfo: function() {
    var that = this;
    var uid = this.data.uid;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetMyPartnerInfo&UID=" + uid),
      success: function(res) {
        // console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data));
        that.setData({
          agreementData: json.ResultData,
        })
      }
    })

  },
  // 接受协议
  agreeAndReceipt: function(e) {
    if (e.detail.value.length == 0) {
      this.setData({
        agreeAndReceipt: "0",
        opacity: 0.5
      })
    } else {
      this.setData({
        agreeAndReceipt: "1",
        opacity: 1
      })
    }
  },

  // 同意条款
  agreeBtnTap: function() {
    var that = this;
    var uid = this.data.uid;
    var isPartnerEnterIsDefaultPhone = this.data.isPartnerEnterIsDefaultPhone;
    var partnerEnterDefaultPhone = this.data.partnerEnterDefaultPhone;
    var operaterPhone = this.data.operaterPhone;
    var orderNumber = this.data.orderNumber;
    var payFlag = this.data.payFlag;
    var agreeAndReceipt = this.data.agreeAndReceipt;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (operaterPhone == "") {
      wx.showToast({
        title: "请输入运营中心或创客手机号！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (!myreg.test(operaterPhone)) {
      wx.showToast({
        title: '请输入正确的手机号！',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (agreeAndReceipt == "0") {
      wx.showToast({
        title: "请同意并接受以上协议！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=ApplyPartner&UID=" + uid + "&OperaterPhone=" + operaterPhone + "&IsPartnerEnterIsDefaultPhone=" + isPartnerEnterIsDefaultPhone + "&PartnerEnterDefaultPhone=" + partnerEnterDefaultPhone),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          that.setData({
            payMoney: parseFloat(json.ResultData.PartnerYearFee).toFixed(2),
          })
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=CreatePartnerOrder&UID=" + uid + "&OperaterPhone=" + operaterPhone),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data))
              console.log(json);
              if (json.ResultFlag == 1) {
                orderNumber= json.ResultData;
                that.setData({
                  orderNumber: orderNumber,
                  payFlag: false
                })

              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }

            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 
  createOrder1: function() {
    this.setData({
      payFlag: true
    })
  },
  //余额或微信 
  radioChange: function(e) {
    // console.log(e);
    this.setData({
      payType: e.detail.value
    });
  },
  // 付款
  goPay: function() {
    var that = this;
    var payType = this.data.payType;
    var orderNumber = this.data.orderNumber;
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    if (payType == '') {
      wx.showToast({
        title: "请选择支付方式",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    this.setData({
      payFlag: true
    })
    if (payType == "0") {
      // 余额
      if (isAlreadyPayPassword == '1') {
        that.setData({
          showPayPwdInput: true,
          payFocus: true
        })
      } else {
        wx.showToast({
          title: "请先去设置支付密码！",
          icon: 'none',
          duration: 2000
        });
      }
    } else {
      // 微信
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=PayPartnerOrder&OrderNumber=" + orderNumber + "&Type=2"),
        success: function(res) {
          var json = JSON.parse(baseJs.decryUrl(res.data))
          console.log(json);
          if (json.ResultFlag == 1) {
            // wx.requestPayment({
            //   timeStamp: json.ResultData.timestamp,
            //   nonceStr: json.ResultData.nonce_str,
            //   package: 'prepay_id=' + json.ResultData.prepay_id,
            //   signType: 'MD5',
            //   paySign: json.ResultData.sign,
            //   success(res) {
            //     console.log(res);

            //   },
            //   fail(res) {
            //     console.log(res);
            //     wx.showToast({
            //       title: res.err_desc,
            //       icon: 'none',
            //       duration: 2000
            //     });
            //   }
            // })
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }
        }
      })

    }
  },
  // 
})